//
//  LevelViewController.swift
//  CTCollectionViewDemo
//
//  Created by GodCoder on 2019/8/6.
//  Copyright © 2019 CTCollectionView. All rights reserved.
//

import UIKit

class LevelViewController: UIViewController {

    let cellSize = CGSize(width: 80, height: 80)
    
    lazy var collectionView: UICollectionView = {
        
        let bgImages = UIImageView.init(frame: self.view.frame)
        bgImages.image = UIImage.init(named: "5")
        
        let layout = HeaderDragLayout.init()
        layout.itemSize = cellSize
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
//        layout.scrollDirection = .horizontal
        
        let cv                    = UICollectionView.init(frame : self.view.frame, collectionViewLayout : layout)
        cv.delegate               = self
        cv.dataSource             = self
        cv.dragDelegate           = self
        cv.dropDelegate           = self
        cv.dragInteractionEnabled = true
        cv.backgroundView         = bgImages
        cv.register(UINib.init(nibName       : "LevelCell", bundle       : nil), forCellWithReuseIdentifier       : "LevelCellID")
        return cv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !UIDevice.current.isGeneratingDeviceOrientationNotifications {
            UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        }
        NotificationCenter.default.addObserver(self, selector: #selector(changeScreen), name: UIDevice.orientationDidChangeNotification, object: nil)
        view.backgroundColor = .white
        view.addSubview(collectionView)
    }
    
    @objc func changeScreen() {
        collectionView.frame = self.view.frame
        collectionView.reloadData()
    }
}

extension LevelViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 150
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LevelCellID", for: indexPath) as! LevelCell
        cell.lab.text = "\(indexPath.section) , \(indexPath.row)"
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldSpringLoadItemAt indexPath: IndexPath, with context: UISpringLoadedInteractionContext) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
        return proposedContentOffset
    }
    
    func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
        return proposedIndexPath
    }
}

extension LevelViewController: UICollectionViewDragDelegate, UICollectionViewDropDelegate {
    func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let itemProvider = NSItemProvider.init()
        let drag = UIDragItem.init(itemProvider: itemProvider)
        return [drag]
    }
    
    /** 被选中以后可执行的操作:1、设置贝塞尔曲线 2、设置背景颜色 */
    func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
        let path = UIBezierPath.init(roundedRect: CGRect(origin: CGPoint(x: 0, y: 0), size: cellSize), cornerRadius: 18)
        let parameters = UIDragPreviewParameters.init()
        parameters.backgroundColor = UIColor.red
        parameters.visiblePath = path
        
        return parameters
    }
    
    func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
        let itemProvider = NSItemProvider.init()
        let drag = UIDragItem.init(itemProvider: itemProvider)
        
        return [drag]
    }
    
    func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) {

    }
    
    func collectionView(_ collectionView: UICollectionView, dragSessionWillBegin session: UIDragSession) {
        
    }
    
    func collectionView(_ collectionView: UICollectionView, dragSessionAllowsMoveOperation session: UIDragSession) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, dragSessionIsRestrictedToDraggingApplication session: UIDragSession) -> Bool {
        return true
    }
    
 
    // MARK:---------------  UICollectionViewDropDelegate ---------------
    func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
    
        let dropProposal = UICollectionViewDropProposal.init(operation: .move, intent: .insertAtDestinationIndexPath)
//        let movePoint = session.location(in: collectionView)
        return dropProposal
    }
    
    func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
        
    }
    
    func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
        guard let endIndexPath: IndexPath = coordinator.destinationIndexPath else {return}
        guard let beginIndexPath: IndexPath = coordinator.items.first?.sourceIndexPath else {return}
        collectionView.moveItem(at: beginIndexPath, to: endIndexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, dropSessionDidEnter session: UIDropSession) {

    }
}
